package com.service;

import com.controller.ShootGame;
import com.domain.*;

import java.awt.*;
import java.util.ArrayList;

public class BulletService {

    private ArrayList<ArrayList<Bullet>> bullets;//声明子弹数组;
    private int number;//控制飞行物数量

    public ArrayList<ArrayList<Bullet>> getBullets() {
        return bullets;
    }

    public BulletService() {
        this.bullets = new ArrayList<>();//创建子弹数组
    }

    /**画子弹**/
    public void paintBullet(Graphics g){//g:一只画笔
        for (ArrayList<Bullet>  bulletList : bullets) {
            for (Bullet bullet : bulletList) {
                //画子弹
                g.drawImage(bullet.getImage(),bullet.getX(),bullet.getY(),null);
            }
        }
    }

    /**
     * 创建子弹
     */
    public void  newBullet(Hero hero){
        //控制子弹数量
        if (number++ %4 == 0){
            //添加子弹集合
            bullets.add(hero.launch());
        }
    }

    /**
     * 让所有子弹移动
     */
    public void moveBullet(){
        //让所有子弹移动
        for (ArrayList<Bullet>  bulletList : bullets) {
            for (Bullet bullet : bulletList) {
                bullet.move();
                //判断子弹是否飞出窗体，是则删除
                if (bullet.getY() < 0){
                    bulletList.remove(bullet);
                    break;
                }
            }
        }
    }

    /**
     * 飞行物碰撞判断
     */
    public void crashAction(ArrayList<Flying> flyings){
        //判断子弹碰撞
        for (ArrayList<Bullet>  bulletList : bullets) {
            for (Bullet bullet : bulletList) {
                //判断子弹是否击中敌机或大飞机
                if (flierCrash(flyings , bullet)){
                    //删除子弹
                    bulletList.remove(bullet);
                    break;
                }
            }
        }
    }

    /**
     * 子弹碰撞方法
     * //碰左    x < x2 + w2
     * //碰右    x + w > x2
     * //碰上    y < y2 +h2
     * //碰下    y + h > y2
     * @param flyings
     * @param bullet
     * @return
     */
    public boolean flierCrash(ArrayList<Flying> flyings , Bullet bullet){
        for (Flying flying : flyings) {
            //飞行物的坐标及宽高
            int x2 = flying.getX();
            int y2 = flying.getY();
            int w2 = flying.getWidth();
            int h2 = flying.getHeight();
            //判断是否发生碰撞
            if (bullet.getX() < x2+w2 && bullet.getX()+bullet.getWidth() > x2 && bullet.getY() < y2+h2 && bullet.getY()+bullet.getHeight() > y2){
                //判断飞行物类型
                if (flying instanceof Airplane){
                    flying.setLifr(flying.getLifr()-1);
                    //飞行物被击杀
                    if (flying.getLifr()<=0){
                        //分数加3
                        ShootGame.score+=3;
                        //当敌机或大飞机生命为0时删除敌机或大飞机
                        flyings.remove(flying);
                        //爆炸效果
                        flyings.add(new Bom(x2,y2,w2,h2));
                    }
                    return true;
                }else if (flying instanceof Bigplane){
                    flying.setLifr(flying.getLifr()-1);
                    //飞行物被击杀
                    if (flying.getLifr()<=0){
                        ShootGame.score+=5;
                        //当敌机或大飞机生命为0时删除敌机或大飞机
                        flyings.remove(flying);
                        //爆炸效果
                        flyings.add(new Bom(x2,y2,w2,h2));
                        //创建蜜蜂,奖励
                        flyings.add(new Bee());
                        flyings.add(new Bee());
                        flyings.add(new Bee());
                    }
                    return true;
                }
//            else if (flying instanceof Bee){
//                if (flying.lifr<=0){
//                    ShootGame.score+=1;
//                }
//                return true;
//            }
//            return true;
            }
        }
        return false;
    }

}
